#define SAF_PROGRAM_NAME "meow"
#include "saf.h"

#define PADDLE_SPEED 3
#define DELAYTIMEUNTILBALLVISIBLE 25 * 1
#define DELAYTIMEUNTILBALLRUN 25 * 2


int8_t ball[2];
int8_t ballold[2];
int8_t ballv[2];
uint8_t ballOut = 1;

int8_t paddle1[2];
int8_t paddle2[2];

uint8_t paddleWidth = 2;
uint8_t paddleHeight = 6;

uint8_t paddleWallDistance;

uint8_t delayBallSpawnCurrent = 0;

uint8_t player1Score = 0;
uint8_t player2Score = 0;



void GAME_clampBall(int8_t ball_in[2], int8_t ballv_in[2])
{
  // Check if ball collided with top region of the screen
  if (ball_in[1] < 0)
  {
    ball_in[1] = 0;
    ballv_in[1] *= -1;
  }
  // Check if ball collided with bottom region of the screen
  else if (ball_in[1] >= SAF_SCREEN_HEIGHT)
  {
    ball_in[1] = SAF_SCREEN_HEIGHT - 1;
    ballv_in[1] *= -1;
  }
}


void GAME_drawPaddle(int8_t paddle_in[2], uint8_t ballHit_in)
{
  SAF_drawRect(paddle_in[0],     // position x
                paddle_in[1], // position y
                paddleWidth, // position width
                paddleHeight, // position height
		// color the paddle red if paddle was hit by a ball
                ballHit_in == 0 ? SAF_COLOR_WHITE : SAF_COLOR_RED, 
                1); // Make the paddle filled rectangle
}

void SAF_init(void)
{
  paddleWallDistance = 2;

  paddle1[0] = paddleWallDistance, 
	  paddle1[1] = (SAF_SCREEN_HEIGHT / 2) - (paddleHeight / 2);
  paddle2[0] = SAF_SCREEN_WIDTH - paddleWallDistance - paddleWidth, 
	  paddle2[1] = paddle1[1];

  
		  
  ball[0] = SAF_SCREEN_WIDTH / 2, ball[1] = SAF_SCREEN_HEIGHT / 2;
  ballv[0] = 1, ballv[1] = 1;
}

uint8_t SAF_loop()
{

  ballold[0] = ball[0], ballold[1] = ball[1];
  uint8_t ballHit1 = 0, ballHit2 = 0;


  // Paddle 1
  if (SAF_buttonPressed(SAF_BUTTON_UP))
  {
    paddle1[1] -= PADDLE_SPEED;
    if (paddle1[1] < 0)
      paddle1[1] = 0;
  }
  if (SAF_buttonPressed(SAF_BUTTON_DOWN))
  {
    paddle1[1] += PADDLE_SPEED;
    if (paddle1[1] + paddleHeight >= SAF_SCREEN_HEIGHT)
      paddle1[1] = SAF_SCREEN_HEIGHT - paddleHeight;
  }

  // Paddle 2
  if (SAF_buttonPressed(SAF_BUTTON_B))
  {
    paddle2[1] -= PADDLE_SPEED;
    if (paddle2[1] < 0)
      paddle2[1] = 0;
  }
  if (SAF_buttonPressed(SAF_BUTTON_A))
  {
    paddle2[1] += PADDLE_SPEED;
    if (paddle2[1] + paddleHeight >= SAF_SCREEN_HEIGHT)
      paddle2[1] = SAF_SCREEN_HEIGHT - paddleHeight;
  }



  if (ballOut == 0)
  {
    // Apply ball's velocity
    ball[0] += ballv[0];
    ball[1] += ballv[1];
  }
  else
  {
    delayBallSpawnCurrent += 1;
    if (delayBallSpawnCurrent > DELAYTIMEUNTILBALLRUN)
    {
      delayBallSpawnCurrent = 0;
      ballOut = 0;
    }
  }


  // Check collision between ball and paddle 1
  if (ball[0] < paddle1[0] + paddleWidth &&
    ballold[0] >= paddle1[0] &&
    ball[1] < paddle1[1] + paddleHeight &&
    ball[1] >= paddle1[1])
  {
    ball[0] = paddle1[0] + paddleWidth + 1;
    ballv[0] *= -1;
    ballHit1 = 1;
    SAF_playSound(SAF_SOUND_BEEP);
  }

 
  // Check collision between ball and paddle 2
  if (ball[0] >= paddle2[0] &&
    ballold[0] < paddle2[0] &&
    ball[1] < paddle2[1] + paddleHeight &&
    ball[1] >= paddle2[1])
  {
    ball[0] = paddle2[0] - 1;
    ballv[0] *= -1;
    ballHit2 = 1;
    SAF_playSound(SAF_SOUND_BEEP);
  }
 



  /* Check if ball went outside the screen through
     left or right side.                           */ 
  if (ball[0] < 0)
  {
    ball[0] = SAF_SCREEN_WIDTH / 2;
    ball[1] = SAF_SCREEN_HEIGHT / 2;
    ballv[0] *= -1;
    ballOut = 1;
    player2Score += 1;
    SAF_playSound(SAF_SOUND_BOOM);
  }
  if (ball[0] >= SAF_SCREEN_WIDTH)
  {
    ball[0] = SAF_SCREEN_WIDTH / 2;
    ball[1] = SAF_SCREEN_HEIGHT / 2;
    ballv[0] *= -1;
    ballOut = 1;
    player1Score += 1;
    SAF_playSound(SAF_SOUND_BOOM);
  }
  
  GAME_clampBall(ball, ballv);


  /////////////////////////
  //// Draw code below ////
  /////////////////////////

  SAF_clearScreen(SAF_COLOR_BLUE_DARK);


  GAME_drawPaddle(paddle1, ballHit1);
  GAME_drawPaddle(paddle2, ballHit2);

  
  if (delayBallSpawnCurrent > DELAYTIMEUNTILBALLVISIBLE || !ballOut)
    SAF_drawPixel(ball[0], ball[1], SAF_COLOR_WHITE);


  char player1ScoreStr[3];
  SAF_intToStr(player1Score, player1ScoreStr);

  char player2ScoreStr[3];
  SAF_intToStr(player2Score, player2ScoreStr);

  SAF_drawText(player1ScoreStr, 0, 0, SAF_COLOR_WHITE, 1);
  SAF_drawText(player2ScoreStr, player2Score < 10 ? 60 : 54, 0, SAF_COLOR_WHITE, 1);

  /*
  SAF_drawPixel(SAF_SCREEN_WIDTH / 2, 
		  SAF_SCREEN_HEIGHT / 2, 
		  SAF_COLOR_RED);
		  */

  return 1;
}
